//
//  ImageShape.m
//  QuartzStudy
//
//  Created by longhuihu on 11-6-22.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ImageShape.h"
#import "BaseShape.h"

@interface ImageShapeBase : BaseShape
{
	CGImageRef image;
}
-(void)setImage:(UIImage *)aImage;
@end

@implementation ImageShapeBase

-(void)setImage:(UIImage *)aImage
{
	image = CGImageCreateCopy([aImage CGImage]);
}
-(void)drawRect:(CGRect)rect
{
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGFloat width = self.bounds.size.width;
	CGFloat height = self.bounds.size.height;
	CGFloat radius = MIN(width/2,height/2);
	CGFloat angle1 = 2*M_PI-(30.0/180.0)*M_PI;
	CGFloat angle2 = 2*M_PI-(270.0/180.0)*M_PI;;
	CGContextBeginPath(context);
	CGPoint centerP = CGPointMake(width/2 , height/2);
	CGContextMoveToPoint(context, centerP.x,centerP.y);
	CGPoint point1 = CGPointMake(centerP.x+radius*cos(angle1),centerP.y + radius*sin(angle1));
	CGContextAddLineToPoint(context, point1.x, point1.y);
	CGContextAddArc(context, centerP.x, centerP.y, radius, angle1, angle2,1);
	CGContextAddLineToPoint(context,centerP.x, centerP.y);	
	CGContextClip(context);
	
	CGContextDrawImage(context, CGRectMake(5, 5, 90, 90), image);
	[super drawFrame:context];
}
-(void)dealloc
{
	[super dealloc];
	CFRelease(image);
}
@end


@interface SubImage : ImageShapeBase
@end

@implementation SubImage
-(void)setImage:(UIImage *)aImage
{
	if (image!=NULL) {
	CFRelease(image);
	image = NULL;
	}
	image = CGImageCreateWithImageInRect(aImage.CGImage,CGRectMake(50, 50, 90, 90));
}
@end


@interface ImageContext : ImageShapeBase
@end
@implementation ImageContext
-(void)setImage:(UIImage *)aImage
{
	if (image!=NULL) {
		CFRelease(image);
		image = NULL;
	}
	int width = 90;
	int height = 90;
	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	image = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
}
@end


@interface MaskWithGradient : ImageShapeBase
@end
@implementation MaskWithGradient
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;

	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	CGImageRef bitmapImage= CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	// Our gradient is always black-white and the mask
	// must be in the gray colorspace
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	// create the bitmap context
    CGContextRef BitmapContext = CGBitmapContextCreate (NULL, width, height,
																8, 0, colorSpace, kCGImageAlphaNone);
	if (BitmapContext != NULL) {
		
		// gradient mask
		CGFloat colors[] = {0.0, 1.0,1.0, 1.0,};
		CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
		CGPoint gradientStartPoint = CGPointZero;
		CGPoint gradientEndPoint = CGPointMake(0,height);
		CGContextDrawLinearGradient (BitmapContext, grayScaleGradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsAfterEndLocation);
		CGGradientRelease(grayScaleGradient);
		CGImageRef gradientMask=CGBitmapContextCreateImage(BitmapContext);
		image = CGImageCreateWithMask(bitmapImage,gradientMask);		
		CFRelease(gradientMask);
		CGContextRelease(BitmapContext);
	}	
	// clean up the colorspace
	CGColorSpaceRelease(colorSpace);	
	CFRelease(bitmapImage);
}
@end

@interface MaskWithImage : ImageShapeBase
@end
@implementation MaskWithImage
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;

	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	CGImageRef bitmapImage = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	// Our gradient is always black-white and the mask
	// must be in the gray colorspace
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	// create the bitmap context
    CGContextRef BitmapContext = CGBitmapContextCreate (NULL, width, height,
														8, 0, colorSpace, kCGImageAlphaNone);
	if (BitmapContext != NULL) {
	
		//ellipse image mask
		CGFloat color1[] = {1.0,1.0};
		CGContextSetFillColor(BitmapContext, color1);
		CGContextFillEllipseInRect(BitmapContext,CGRectMake(0, 0, width, height));
		CGImageRef imageMask = CGBitmapContextCreateImage(BitmapContext);
		image = CGImageCreateWithMask(bitmapImage,imageMask);		
		CFRelease(imageMask);
		CGContextRelease(BitmapContext);
	}	
	// clean up the colorspace
	CGColorSpaceRelease(colorSpace);	
	CFRelease(bitmapImage);
}
@end

@interface MaskWithMask : ImageShapeBase
@end
@implementation MaskWithMask
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;

	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	CGImageRef bitmapImage = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	// Our gradient is always black-white and the mask
	// must be in the gray colorspace
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	// create the bitmap context
    CGContextRef BitmapContext = CGBitmapContextCreate (NULL, width, height,
														8, 0, colorSpace, kCGImageAlphaNone);
	if (BitmapContext != NULL) {
		
		//ellipse image mask
		CGFloat color1[] = {1.0,1.0};
		CGContextSetFillColor(BitmapContext, color1);
		CGContextFillEllipseInRect(BitmapContext,CGRectMake(0, 0, width, height));
		CGImageRef imageMask = CGBitmapContextCreateImage(BitmapContext);
		
		
		CGDataProviderRef provider = CGImageGetDataProvider(imageMask);
		/*
		 * bytesPerRow并不等于bitsPerPixel/8*width，而多了几个字节，不知到是干什么用的
		 */
		size_t bytesPerRow = CGImageGetBytesPerRow(imageMask);
		CGImageRef mask = CGImageMaskCreate(width, height, 8, 8, bytesPerRow, provider, NULL, true);
		image= CGImageCreateWithMask(bitmapImage,mask);	
		CFRelease(mask);
		
		CFRelease(imageMask);
		CGContextRelease(BitmapContext);
	}	
	// clean up the colorspace
	CGColorSpaceRelease(colorSpace);	
	CFRelease(bitmapImage);
}
@end


@interface MaskWithColor : ImageShapeBase
@end
@implementation MaskWithColor
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;
	/*
	 此处创建了没有alpha componentbitmap context：第二个参数，原因是CGImageCreateWithMaskingColors不能用于带alpha的image	
	 */
	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),YES,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	CGImageRef bitmapImage = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	const float colors[6] = {0,255,128,255,0,255};
	image = CGImageCreateWithMaskingColors(bitmapImage, colors);
	
	CFRelease(bitmapImage);
}
@end


@interface ContextMask : ImageShapeBase
@end
@implementation ContextMask
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;
	
	// Our gradient is always black-white and the mask
	// must be in the gray colorspace
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	// create the bitmap context
    CGContextRef BitmapContext = CGBitmapContextCreate (NULL, width, height,
														8, 0, colorSpace, kCGImageAlphaNone);
	//ellipse image mask
	CGFloat color1[] = {1.0,1.0};
	CGContextSetFillColor(BitmapContext, color1);
	CGContextFillEllipseInRect(BitmapContext,CGRectMake(0, 0, width, height));
	CGImageRef imageMask = CGBitmapContextCreateImage(BitmapContext);
	CGContextRelease(BitmapContext);
	CGColorSpaceRelease(colorSpace);	
	
	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGContextClipToMask(context, CGRectMake(0,0,width,height), imageMask);
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	image = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	// clean up the colorspace
	CFRelease(imageMask);

}
@end

@interface BlendImage : ImageShapeBase
@end
@implementation BlendImage
-(void)setImage:(UIImage *)aImage
{
	int width = 90;
	int height = 90;
	
	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	CGContextRef context = UIGraphicsGetCurrentContext();
	CGFloat color[] = {0.1,1.0,0.7,1};
	CGContextSetFillColor(context, color);
	CGContextFillEllipseInRect(context,CGRectMake(0, 0, width, height));
	CGImageRef  imageMask= CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height),NO,0.0);
	context = UIGraphicsGetCurrentContext();
	CGContextSetBlendMode(context, kCGBlendModeLuminosity);
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),imageMask);
	CGContextDrawImage(context, CGRectMake(0, 0, width,height),[aImage CGImage]);
	image = CGBitmapContextCreateImage(context);
	UIGraphicsEndImageContext();
	
	// clean up the colorspace
	CFRelease(imageMask);	
}
@end

@implementation ImageShape

- (id)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code.
		rawImage = [UIImage imageNamed:@"image.png"];
		
		SubImage *sub = [[SubImage alloc] initWithFrame:CGRectMake(0, 0, 100,100)];
		[sub setImage:rawImage];
		[self addSubview:sub];
		[sub release];
		
		ImageContext *imageContext = [[ImageContext alloc] initWithFrame:CGRectMake(100, 0, 100,100)];
		[imageContext setImage:rawImage];
		[self addSubview:imageContext];
		[imageContext release];
		
		MaskWithImage *withImage = [[MaskWithImage alloc] initWithFrame:CGRectMake(200, 0, 100,100)];
		[withImage setImage:rawImage];
		[self addSubview:withImage];
		[withImage release];
		
		MaskWithGradient *withGradient = [[MaskWithGradient alloc] initWithFrame:CGRectMake(0, 110, 100,100)];
		[withGradient setImage:rawImage];
		[self addSubview:withGradient];
		[withGradient release];
		
		MaskWithMask *withMask = [[MaskWithMask alloc] initWithFrame:CGRectMake(100, 110, 100,100)];
		[withMask setImage:rawImage];
		[self addSubview:withMask];
		[withMask release];		
		
		MaskWithColor *withColor= [[MaskWithColor alloc] initWithFrame:CGRectMake(200, 110, 100,100)];
		[withColor setImage:rawImage];
		[self addSubview:withColor];
		[withColor release];
		
		ContextMask *cMask= [[ContextMask alloc] initWithFrame:CGRectMake(200, 110, 100,100)];
		[cMask setImage:rawImage];
		[self addSubview:cMask];
		[cMask release];
		
		BlendImage *bImage= [[BlendImage alloc] initWithFrame:CGRectMake(0, 220, 100,100)];
		[bImage setImage:rawImage];
		[self addSubview:bImage];
		[bImage release];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code.
}
*/

- (void)dealloc {
    [super dealloc];
	[rawImage release];
}


@end
